//左旋转字符串: https://www.nowcoder.com/practice/12d959b108cb42b1ab72cef4d36af5ec?
//方法一，一个一个迁移
class Solution {
  public:
    void LeftRotateStringOne(string& str) {
        char temp = str[0];
        int len = str.size();
        int i = 0;
        for (; i < len - 1; i++) 
        {
            str[i] = str[i + 1];
        }
        str[i] = temp;
    }
    string LeftRotateString(string str, int n) {
        if (n == 0 || str.empty()) 
        {
            return str;
        }
        n %= str.size();
        while (n) 
        {
            LeftRotateStringOne(str);
            n--;
        }
        return str;
    }
};
//方法二，先局部逆置，在整体逆置
class Solution {
  public:
    void ReverseString(string& str, int start, int end) {
        while (start < end) 
        {
            char temp = str[start];
            str[start] = str[end];
            str[end] = temp;
            start++, end--;
        }
    }
    string LeftRotateString(string str, int n) 
    {
        if (n == 0 || str.empty()) 
        {
            return str;
        }
        n %= str.size(); //去除重复移动
        ReverseString(str, 0, n - 1); //前半部分逆置
        ReverseString(str, n, str.size() - 1); //后半部分逆置
        ReverseString(str, 0, str.size() - 1); //整体逆置
        return str;
    }
};